<template>
  <view class="cart-container" v-if="getCarList.length > 0">
    <!-- 收货地址子组件 -->
    <my-address />
    <view class="cart-title">
      <uni-icons type="shop" size="17"></uni-icons>
      <text class="cart-title-text">购物车</text>
    </view>
    <uni-swipe-action>
      <!-- v-for加在uni-swipe-action-item上才不会左滑都滑 -->
      <uni-swipe-action-item
        :right-options="options"
        @click="onDel(item.goods_id)"
        v-for="item in getCarList"
        :key="item.goods_id"
      >
        <my-goods :goods="item">
          <!-- 左边radio具名插槽 -->
          <template #radio>
            <radio
              :checked="item.goods_state"
              @click.stop="changeState(item.goods_id, !item.goods_state)"
              color="#c00000"
            />
          </template>
          <!-- 右边number具名插槽 -->
          <template #number>
            <!-- 这个第三方还没有解决冒泡问题， @click.stop=""外面包的父元素来解决 -->
            <view @click.stop="">
              <!-- 点击 + - 需要两个值 即item.goods_id,goods_number--》item.goods_id,
              $event -->
              <!-- $event 代表的是uni-number-box 底层给我们传过来的值 （这里是数量goods_number ）-->
              <uni-number-box
                v-model="item.goods_number"
                :min="1"
                @change="changeNumber(item.goods_id, $event)"
              ></uni-number-box>
            </view>
          </template>
        </my-goods>
      </uni-swipe-action-item>
    </uni-swipe-action>
    <my-settle />
  </view>
  <view v-else class="empty-cart">
    <image
      src="/static/cart_empty@2x.png"
      mode="scaleToFill"
      class="empty-img"
    />
    <text class="tip-text">空空如也</text>
  </view>
</template>


<script>
import tabBarBadge from "@/mixins/tabBar-badge";
import { mapGetters, mapMutations } from "vuex";
export default {
  mixins: [tabBarBadge],
  data() {
    return {
      options: [
        {
          text: "删除",
          style: {
            backgroundColor: "#c00000",
          },
        },
      ],
    };
  },
  computed: {
    ...mapGetters("car", ["getCarList"]),
  },
  onLoad() {
    // console.log("getCarList:", this.getCarList);
  },
  methods: {
    ...mapMutations("car", [
      "updateGoodsState",
      "updateGoodsNumber",
      "deleGoodsById",
    ]),
    // 滑动删除
    onDel(goods_id) {
      this.deleGoodsById(goods_id);
    },
    // 更改商品数量 + -
    changeNumber(goods_id, goods_number) {
      console.log(goods_id, goods_number);
      this.updateGoodsNumber({ goods_id, goods_number });
    },
    changeState(goods_id, goods_state) {
      // 点击radio改变状态
      this.updateGoodsState({ goods_id, goods_state });
    },
  },
};
</script>


<style lang="scss" scoped>
.cart-container {
  padding-bottom: 50px;
  .cart-title {
    height: 40px;
    display: flex;
    align-items: center;
    font-size: 14px;
    padding-left: 5px;
    border-bottom: 1px solid #efefef;
    .cart-title-text {
      margin-left: 10px;
    }
  }
}
.empty-cart {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 150px;

  .empty-img {
    width: 90px;
    height: 90px;
  }

  .tip-text {
    font-size: 12px;
    color: gray;
    margin-top: 15px;
  }
}
</style>
